import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import IP2Region from "ip2region";
import * as _ from 'lodash';

@Injectable()
export class IpService {
  constructor(private readonly http: HttpService) {}
   /**
   * 根据IP获得请求地址
   * @param ip 为空时则为当前请求的IP地址
   */
  async getAddress(ip: string) {
    try {
      const query = new IP2Region();
      const res:any = query.search('ip');
      const address = res.country + res.province + res.city;
      return address;
    }catch (err) {
      return '无法获取地址信息';
    }
  }
}
